/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLElement;

public class SkeletonItem extends ReasoningItem implements IElement {

    private enum MySubclass { Let, Conclusion, Assume, Given, Take, TakeAsVar,}

    public static boolean isSkeletonItem(String className){
        boolean found = false;
        for (MySubclass mySubclass :MySubclass.values()){
            if (mySubclass.name().equals(className)){
                found = true;
                break;
            }
        }
        return found;
    }

    private INode parentNode;

    private IElement element = null;
    private Thesis thesis = null;

    public static SkeletonItem newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        SkeletonItem skeletonItem = new SkeletonItem();
        skeletonItem.parentNode = parentNode;
        XMLElement xmlElement = xmlElementList.peek();
        if (!isSkeletonItem(xmlElement.getName())){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not an element of SkeletonItem", xmlElementList);
        }
        MySubclass mySubclass = MySubclass.valueOf(xmlElement.getName());
        switch (mySubclass) {
        case Assume: 
        	skeletonItem.element = Assume.newInstance(parentNode, xmlElementList);
        	break;
        case Conclusion: 
        	skeletonItem.element = Conclusion.newInstance(parentNode, xmlElementList);
        	break;
        case Given: 
        	skeletonItem.element = Given.newInstance(parentNode, xmlElementList);
        	break;
        case Let: 
        	skeletonItem.element = Let.newInstance(parentNode, xmlElementList);
        	break;
        case Take: 
        	skeletonItem.element = Take.newInstance(parentNode, xmlElementList);
        	break;
        case TakeAsVar: 
        	skeletonItem.element = TakeAsVar.newInstance(parentNode, xmlElementList);
        	break;
        default: throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not an element of SkeletonItem", xmlElementList);
        }
        xmlElement = xmlElementList.peek();
        if (xmlElement.getName().equals("Thesis")){
        	skeletonItem.thesis = Thesis.newInstance(parentNode, xmlElementList);
        }
        return skeletonItem;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        xmlElementList.addAll(this.element.getXMLElementList());
        if (this.thesis != null){
            xmlElementList.addAll(this.thesis.getXMLElementList());
        }
        return xmlElementList;
    }

	@Override
	public INode getParentNode() {
		return this.parentNode;
	}

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}
	
}
